package entity;

public class agent{
	board b;   //This agent is on the board b.
	private int x,y;   
	
	public int getX() {
		return x;
	}
	public int getY() {
		return y;
	}
	public void setX(int x) {
		this.x = x;
	}
	public void setY(int y) {
		this.y = y;
	}
	public int[] moveCostTable;//Store the move cost,sequence:UP,RIGHT,DOWN,LEFT
	
	/*constructors
	 */
	public agent(){
		x=0;
		y=0;
	}
	public agent(board board,int x,int y){
		this.b=board;
		this.x=x;
		this.y=y;
	}
	public agent(board board,int x,int y,int[] moveCostTable){
		this.b=board;
		this.x=x;
		this.y=y;
		this.moveCostTable=moveCostTable;
	}
	
	/* test if the agent can move along the given direction
	 * return true or false;
	 * */
	public boolean canMove(moveDirection direction){
		if(direction.equals(moveDirection.UP)&&b.isAccessible(x, y-1))
			return true;
		if(direction.equals(moveDirection.RIGHT)&&b.isAccessible(x+1, y))
			return true;
		if(direction.equals(moveDirection.DOWN)&&b.isAccessible(x,y+1))
			return true;
		if(direction.equals(moveDirection.LEFT)&&b.isAccessible(x-1,y))
			return true;
		return false;
	}
	
	/* move the agent
	 * return the move cost
	 * return 0 stands that the movement can't be executed
	 * */
	public int move(moveDirection direction){
		if(direction==moveDirection.UP&&canMove(moveDirection.UP)){
			y--;
			return moveCostTable[0];
		}else if(direction==moveDirection.RIGHT&&canMove(moveDirection.RIGHT)){
			x++;
			return moveCostTable[1];
		}else if(direction==moveDirection.DOWN&&canMove(moveDirection.DOWN)){
			y++;
			return moveCostTable[2];
		}else if(direction==moveDirection.LEFT&&canMove(moveDirection.LEFT)){
			x--;
			return moveCostTable[3];
		}
		return 0;
	}
	
	/* move the two agents together
	 * return the cost
	 * return 0 stands that the movement can't be executed
	 */
	public int moveWithAnother(moveDirection direction,agent another){
		if(!canMove(direction))
			return 0;
		if(direction.equals(moveDirection.UP)&&another.canMove(moveDirection.RIGHT)){
			move(direction);
			another.move(moveDirection.RIGHT);
			return moveCostTable[0];
		}else if(direction.equals(moveDirection.RIGHT)&&another.canMove(moveDirection.DOWN)){
			move(direction);
			another.move(moveDirection.DOWN);
			return moveCostTable[1];
		}else if(direction.equals(moveDirection.DOWN)&&another.canMove(moveDirection.LEFT)){
			move(direction);
			another.move(moveDirection.LEFT);
			return moveCostTable[2];
		}else if(direction.equals(moveDirection.LEFT)&&another.canMove(moveDirection.UP)){
			move(direction);
			another.move(moveDirection.UP);
			return moveCostTable[3];
		}
		return 0;
	}
	
	

}
